//
//  MineFeedbackViewController.m
//  ShowYork
//
//  Created by hxh on 2020/10/13.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "MineFeedbackViewController.h"
#import "VideoDescContentCell.h"
#import "VideoOtherContentCell.h"
#import "ReportArgModel.h"
@interface MineFeedbackViewController ()<UITableViewDelegate,UITableViewDataSource,VideoDescContentCellDelegate,VideoOtherContentCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)ReportArgModel *argModel;

@end

@implementation MineFeedbackViewController

- (ReportArgModel *)argModel{
    if (!_argModel) {
        _argModel = [[ReportArgModel alloc]init];
    }
    return _argModel;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self findHairlineImageViewUnder:self.navigationController.navigationBar].hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
   
    [self findHairlineImageViewUnder:self.navigationController.navigationBar].hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    // Do any additional setup after loading the view.
}

- (void)setUI{
    
    self.navigationItem.title = @"意见反馈";
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.estimatedSectionFooterHeight = .0f;
    self.tableView.estimatedSectionHeaderHeight = .0f;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.right.equalTo(self.view.mas_right);
            make.left.equalTo(self.view.mas_left);
        }
        make.bottom.equalTo(self.view);
    }];
    
    self.confirmBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 44)];
    [self.confirmBtn setTitle:@"提交" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:C51 forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:RGBA(204, 204, 204, 1) forState:UIControlStateDisabled];
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    self.confirmBtn.titleLabel.font = font(17);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.confirmBtn];
    self.navigationItem.rightBarButtonItem.enabled = NO;
    
    self.confirmBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    self.view.backgroundColor = RGBA(248, 248, 248, 1);
    self.tableView.backgroundColor = RGBA(248, 248, 248, 1);;
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self.view addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(0.5);
    }];
}

#pragma mark - method

- (void)back{
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)confirmMethod{
    
    [self.view endEditing:YES];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    param[@"userId"] = [RPAppManager getCustomerId];
    
    NSString *contact = @"";
    if (self.argModel.qq.length != 0) {
        contact = [contact stringByAppendingFormat:@"%@/",self.argModel.qq];
    }else{
        contact = [contact stringByAppendingFormat:@"0/"];
    }
    if (self.argModel.phone.length != 0) {
        contact = [contact stringByAppendingFormat:@"%@/",self.argModel.phone];
    }else{
        contact = [contact stringByAppendingString:@"0/"];
    }
    if (self.argModel.mail.length != 0) {
        contact = [contact stringByAppendingFormat:@"%@",self.argModel.mail];
    }else{
        contact = [contact stringByAppendingString:@"0"];
    }
    param[@"contact"] = contact;
    param[@"content"] = self.argModel.desc;
    param[@"md5"] = [RPAppManager md5StrWithApi:AddFeedback];
    [SVProgressHUD show];
        [[RPNetworkExtensionTool sharedTool]requestPOST:kGET_REQUEST_URL(AddFeedback) parames:param success:^(id responseObj) {
        [WHToast showImage:[UIImage imageNamed:@"toast_tag_success"] message:@"感谢您的反馈，我们会努力进步的" duration:2 finishHandler:nil];
            [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [WHToast showImage:[UIImage imageNamed:@"toast_tag_error"] message:error.userInfo[@"info"] duration:2 finishHandler:nil];
    }];
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        VideoDescContentCell *cell = [VideoDescContentCell cellWithTableView:tableView];
        [cell setDescContent:self.argModel.desc maxCount:200 isMine:YES];
        cell.delegate = self;
        return cell;
    }
    if (indexPath.section == 1) {
        VideoOtherContentCell *cell = [VideoOtherContentCell cellWithTableView:tableView];
        if (indexPath.row == 0) {
            [cell setOtherContent:self.argModel.phone type:XHOTHERRERORTPHONE isMine:YES];
        }else if (indexPath.row == 1){
            [cell setOtherContent:self.argModel.qq type:XHOTHERRERORTQQ isMine:YES];
        }else if (indexPath.row == 2){
            [cell setOtherContent:self.argModel.mail type:XHOTHERRERORTMAIL isMine:YES];
        }
        cell.delegate = self;
        return cell;
    }
    return [UITableViewCell new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44.0f;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"问题描述（必填）";
    }
    if (section == 1) {
        return @"联系方式（选填）";
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 170.0f;
    }
    if (indexPath.section == 1) {
        return 60.0f;
    }
    return 50.0;
}

#pragma mark - VideoDescContentCellDelegate
- (void)videoDescContentEndEditing:(NSString *)content{
    self.argModel.desc = content;
}
- (void)videoDescDidChange:(NSString *)content{
    if ([content isSpaceString] || content.length == 0) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    }else{
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }
    
}
#pragma mark - VideoOtherContentCellDelegate
- (void)videoOtherContentEndEditing:(NSString *)content type:(XHOtherReportType)type{
    if (type == XHOTHERRERORTQQ) {
        self.argModel.qq = content;
    }
    if (type == XHOTHERRERORTMAIL) {
        self.argModel.mail = content;
    }
    if (type == XHOTHERRERORTPHONE) {
        self.argModel.phone = content;
    }
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0)
    {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
@end
